package com.ranyk.www.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * CLASS_NAME: KaptchaConfig.java <br/>
 *
 * @author ranyk           <br/>
 * @version V1.0           <br/>
 * @decription: 验证码配置类  <br/>
 * @date: 2022-04-14 <br/>
 */
@Configuration
public class KaptchaConfig {


    @Bean
    public DefaultKaptcha captchaProducer() {
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        kaptcha.setConfig(kaptchaConfig());
        return kaptcha;
    }

    /**
     * 验证码属性配置,注意使用的 Kaptcha 方式生成验证码图片时,配置参数时全部用字符串类型(String) ,<br/>
     * 因为获取配置属性时调用的是 java.util.Properties.java 中的 public String getProperty(String key)  方法,<br/>
     * 该方法会使用如下代码进行获取配置的属性值 String sval = (oval instanceof String) ? (String)oval : null;<br/>
     * 如果不配置为 String 类型则会直接返回 null ,从而使用默认值<br/>
     *
     * @return 返回配置对象 {@link Config}
     */
    private Config kaptchaConfig() {
        Properties properties = new Properties();
        properties.put("kaptcha.border", "no");
        properties.put("kaptcha.textproducer.font.color", "black");
        properties.put("kaptcha.image.width", "85");
        properties.put("kaptcha.image.height", "42");
        properties.put("kaptcha.textproducer.font.size", "30");
        properties.put("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
        properties.put("kaptcha.session.key", "code");
        properties.put("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
        properties.put("kaptcha.background.clear.from", "92,189,170");
        properties.put("kaptcha.background.clear.to", "92,189,170");
        properties.put("kaptcha.textproducer.char.length", "4");
        properties.put("kaptcha.textproducer.font.names", "彩云,宋体,楷体,微软雅黑");
        return new Config(properties);
    }

}
